package ricky.game.graphic
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.filters.BlurFilter;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.GlowFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Vector3D;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Dictionary;
	
	import ricky.Interface.IDispose;
	import ricky.Interface.IGraphic;
	import ricky.animate.AnimScene;
	import ricky.animate.AnimSprite;
	import ricky.cache.ModelCache;
	import ricky.game.Actor;
	import ricky.utils.Utils;

	/**
	 * 游戏复杂对象显示资源。<br>
	 * 
	 */
	public class Graphic extends Sprite implements IDispose,IGraphic//IAnimate
	{
		public function Graphic(character:Actor=null)
		{
			_isDisposed=false;
			this.mouseChildren=false;
			this.mouseEnabled=false;
			this._character=character;
			
		}
		
		private var _rotationPu:Vector3D;
		
		private var _velocityPu:Vector3D;
		
		public function get label():Object
		{
			return _label;
		}

		public function set label(value:Object):void
		{
			_label = value;
		}

		public function get frameSpeed():Number
		{
			return _frameSpeed;
		}

		public function set frameSpeed(value:Number):void
		{
			_frameSpeed = value;
			if(_clothes)
				_clothes.fps=_frameSpeed;
		}

		public function get velocityPu():Vector3D
		{
			return _velocityPu;
		}

		public function set velocityPu(value:Vector3D):void
		{
			_velocityPu = value;
		}

		public function get rotationPu():Vector3D
		{
			return _rotationPu;
		}

		public function set rotationPu(value:Vector3D):void
		{
			_rotationPu = value;
		}

		/**
		 * 对应的游戏逻辑对象
		 */
		public function get actor():Actor
		{
			return _character;
		}

		/**
		 * @private
		 */
		public function set actor(value:Actor):void
		{
			_character = value;
		}

		/**
		 * 主显示对象 
		 */
		public function get clothes():AnimSprite
		{
			return _clothes;
		}
		
		/**
		 * 卸载正在加载的模型资源
		 */
		public function unloadModel(model:String):void
		{
			ModelCache.instance.unloadRes(model);
		}
		
		/**
		 * 穿衣服 换衣服的时候先设置为null然后在换
		 * @param model 模型相对路径 不需要加后缀
		 */
		public function wear(model:String=null,name:String=null,onLoaded:Function=null):void
		{
			if(this._model!=model)
			{
				_model=model;
				if(model==null) //如果为空表示清楚衣服
					wearClothes1(null);
				else
				{
					if(_clothes==null) //如果不存在就创建
					{	
						var clothes:AnimSprite=new AnimSprite();
						clothes.loadModel(model);
						wearClothes1(clothes);
						if(_fps>0)
							clothes.fps=_fps;
					}else
					{
						_clothes.clear();
						_clothes.loadModel(model);
					}
				}
			}
		}

		/**
		 * 追加骨骼动画到指定模型
		 * @param model 模型相对路径 不需要加后缀
		 */
		public function appendAnimation(model:String=null,name:String=null,onLoaded:Function=null):void
		{
			
		}
		
		/**
		 * 清理指定名字的模型和动画
		 */	
		public function clear(name:String=null):void
		{
			
		}
		/**
		 * 穿衣服 会自动移除上次的衣服
		 * @param anima:AnimSprite 动画显示对象
		 */
		private function wearClothes1(clothes:AnimSprite=null):void
		{
			if(this._clothes!=null && this._clothes.parent!=null)
				_clothes.clear();
			
			this._clothes=clothes;
				
			if(_clothes!=null)
			{
				_clothes.defaultScene=_defaultScene;
				this.addChild(_clothes);
			}
		}
		
		/**
		 * 移除所有可显示对象
		 */
		public function remove():void
		{
			while(this.numChildren>0)
				this.removeChild(this.getChildAt(0));
		}
		
		//////////////////接口.///////////////////////////
		
		/**
		 * 得到某个属性的值
		 */
		public function getAttribute(pro:String):*
		{
			return this[pro];
		}
		
		/**
		 * 设置某个属性的值
		 */
		public function setAttribute(pro:String,value:*):void
		{
			this[pro]=value;
		}
		
		/**
		 * 开始播放动画
		 */
		public function playScene(par:*=null):void
		{
			if(_clothes)
			{
				_clothes.playScene(par);
			}
		}
		
		/**
		 * 停止播放动画
		 */
		public function stop():void
		{
			if(_clothes)
				_clothes.stop();
		}
		
		/**
		 * 销毁对像 释放系统资源
		 */
		public function dispose():void
		{
			var child:*;
			
			while(this.numChildren>0)
			{
				child=this.getChildAt(0);
				this.removeChild(child);

				if(child is IDispose)
					IDispose(child).dispose();
			}
			
			_clothes=null;
			_isDisposed=true;
		}
		
		/**
		 * 是否调用过dispose方法
		 */
		public function get isDisposed():Boolean
		{
			return	_isDisposed;
		}
		
		/**
		 * 玩家的名字等提示信息
		 */
		public function get infoTxt():String
		{
			if(_infoTxt!=null)
				return _infoTxt.htmlText;
			else
				return "";
		}
		
		/**
		 * 设置玩家的显示信息支持html格式
		 * @private
		 */
		public function set infoTxt(value:String):void
		{
			if(this._infoTxt==null)
			{
				_infoTxt=new TextField();
				_infoTxt.autoSize="left";
				_infoTxt.defaultTextFormat=new TextFormat(null,12,0xff0f0f);
				this.addChild(_infoTxt);
			}
			_infoTxt.htmlText = value;
			_infoTxt.x=(0-_infoTxt.textWidth)/2;

		}

		/**
		 * 当前动画播放的fps
		 */
		public function get fps():int
		{
			return _fps;
		}

		public function set fps(value:int):void
		{
			_fps = value;
			if(clothes)
				clothes.fps=value;
		}

		public function get model():String
		{
			return _model;
		}		

		/**
		 * 默认场景
		 */
		public function get defaultScene():AnimScene
		{
			return _defaultScene;
		}

		public function set defaultScene(value:AnimScene):void
		{
			_defaultScene = value;
		}

		
		protected var _clothes:AnimSprite;
		
		protected var _character:Actor;
		//
		private var _isDisposed:Boolean=false;

		private var _infoTxt:TextField;
		
		private var _fps:int;

		private var _model:String;
		
		private var _frameSpeed:Number;
		
		private var _label:Object;
		
		private var _defaultScene:AnimScene;
	}
}